/*
 * File :  DefaultActivityManager.java
 * Description : DefaultActivityManager
 * Author :  王建
 * Time :  16-9-28 下午2:38
 *
 * Copyright (C) 2016 Chengdu Ruichuangzhibo Technology Co., Ltd. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wman.sheep.common.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * 作者：王建 on 2016/4/26 16:43
 * 邮箱：wang1991jian@gmial.com
 * 描述：配置管理类-抽象类
 */
public class DefaultActivityManager extends Stack<Activity> implements ActivityManagerImpl {
    public DefaultActivityManager() {
        super();
    }

    /**
     * 添加Activity到堆栈
     */
    @Override
    public void addActivity(Activity activity) {
        super.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    @Override
    public Activity currentActivity() throws Exception {
        if (isEmpty()) {
            throw new Exception("Activity Stack 长度为空");
        }
        return super.lastElement();
    }

    /**
     * 结束指定的Activity
     */
    @Override
    public void finishActivity() {
        try {
            System.out.println("finish");
            Activity activity = this.currentActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束指定的Activity
     */
    @Override
    public void finishActivity(Activity activity) {
        super.remove(activity);
        activity.finish();
    }

    /**
     * 结束指定类名的Activity
     */
    @Override
    public void finishClass(Class<?> cls) {
        for (Activity activity : this) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = super.size(); i < size; i++) {
            Activity activity = super.get(i);
            if (null != activity) {
                activity.finish();
            }
        }
        super.clear();
    }

    @Override
    public void exitApp(Context context) {
        try {
            this.finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            activityMgr.restartPackage(context.getPackageName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
